﻿using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using UnityEngine;
using Yoozoo.UI.YGUI;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientation.Adapter
{
    
    //生成YTextAdaptConfiger
    [System.Serializable]
    public class YTextAdaptConfiger : AdaptBaseConfig
    {
        public YTextStyle style;
        public YTextColorStyle styleColor;
        public TextAnchor alignment = (TextAnchor)DEFAULT_VALUE;
        public float lineSpacing;
        
        //重写Equals方法
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            YTextAdaptConfiger other = (YTextAdaptConfiger)obj;
            // Compare style properties
            if (style != other.style)
            {
                return false;
            }
            
            // Compare styleColor properties
            if (styleColor != other.styleColor)
            {
                return false;
            }

            if (alignment != other.alignment)
            {
                return false;
            }

            if (!LineSpacingEquals(other.lineSpacing))
            {
                return false;
            }

            return true;
        }
        
        //生成GetHashCode方法
        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + style.GetHashCode();
            hash = hash * 23 + styleColor.GetHashCode();
            hash = hash * 23 + alignment.GetHashCode();
            hash = hash * 23 + lineSpacing.GetHashCode();
            return hash;
        }
        
        public bool LineSpacingEquals(float rightValue)
        {
            float diffAbs = Mathf.Abs(lineSpacing - rightValue);
            float tolerance = 1e-5f;
            return diffAbs <= tolerance;
        }
    }
    public class YTextAdapter: AdaptBase<YTextAdaptConfiger, YText> 
    {
        
        // private YImageWrap mImageWrap;
        protected override void CopyProperty(YTextAdaptConfiger config) {
            base.CopyProperty(config);

            config.style = mComponent.style;
            config.styleColor = mComponent.colorStyle;
            config.alignment = mComponent.alignment;
            config.lineSpacing = mComponent.lineSpacing;
        }

        protected override void ApplyConfig(YTextAdaptConfiger config) {
            base.ApplyConfig(config);
            
            mComponent.style = config.style;
            mComponent.colorStyle = config.styleColor;
            if ((int)config.alignment != DEFAULT_VALUE)
            {
                mComponent.alignment = config.alignment;
            }

            if (!config.LineSpacingEquals(default))
            {
                mComponent.lineSpacing = config.lineSpacing;
            }
        }
        
        protected new void OnDestroy() {
            base.OnDestroy();
        }
    }
}